package com.tool.cn.util;

import java.net.URL;

/**
 * 加载类工具类
 * 
 * @author wangzh
 * 2018年7月3日 上午10:39:25
 */
public class ClassUtil {

	  public static ClassLoader getClassLoader(){
	    return Thread.currentThread().getContextClassLoader();
	  }

	  public static String getClassPath(){
	    String classpath = "";
	    URL resource = getClassLoader().getResource("");
	    if (resource != null) {
	      classpath = resource.getPath();
	    }
	    return classpath;
	  }
	  
	  /**
	   * @param className 带包名类名，不带后缀.java,实例com.tool.cn.util.ByteUtil
	   * @return
	   */
	  public static Class<?> loadClass(String className){
	    return loadClass(className, true);
	  }

	  public static Class<?> loadClass(String className, boolean isInitialized){
	    Class<?> cls;
	    try{
	      cls = Class.forName(className, isInitialized, getClassLoader());
	    } catch (ClassNotFoundException e) {
	      System.err.println("加载类出错！");
	      throw new RuntimeException(e);
	    }
	    return cls;
	  }

	  public static boolean isInt(Class<?> type){
	    return (type.equals(Integer.TYPE)) || (type.equals(Integer.class));
	  }

	  public static boolean isLong(Class<?> type){
	    return (type.equals(Long.TYPE)) || (type.equals(Long.class));
	  }

	  public static boolean isDouble(Class<?> type){
	    return (type.equals(Double.TYPE)) || (type.equals(Double.class));
	  }

	  public static boolean isString(Class<?> type){
	    return type.equals(String.class);
	  }
}
